package com.sztx.pay.center.core.service.impl;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sztx.pay.center.common.util.ConfigUtil;
import com.sztx.pay.center.core.service.AccountService;
import com.sztx.pay.center.core.service.MqMessageService;
import com.sztx.pay.center.core.service.PayCoreService;
import com.sztx.pay.center.core.service.bean.BindCardMqMsg;
import com.sztx.se.core.context.SpringContextHolder;
import com.sztx.se.core.mq.producer.MqMessageSender;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.trade.payment.gateway.rpc.api.service.callback.Callback;
import com.sztx.trade.payment.gateway.rpc.api.service.cardbinding.RespBindBankCard;
import com.sztx.trade.payment.gateway.rpc.api.service.cardbinding.RespVerifyBindBankCard;
import com.sztx.trade.payment.gateway.rpc.api.service.distribute.RespDistribute;
import com.sztx.trade.payment.gateway.rpc.api.service.refund.RespRefund;
import com.sztx.trade.payment.gateway.rpc.api.service.trade.RespTrade;
import com.sztx.trade.payment.gateway.rpc.api.service.transfer.RespTransfer;
import com.sztx.trade.payment.gateway.rpc.api.service.withdraw.RespWithdraw;

@Service("mqMessageService")
public class MqMessageServiceImpl extends BaseServiceImpl implements MqMessageService {
	
	@Autowired(required = false)
	private MqMessageSender mqMessageSender;
	
	@Autowired
	private PayCoreService payCoreService;
	@Autowired
	@Qualifier("exchangeTemplate")
	private AmqpTemplate amqpTemplate;
	
	
	
	
	public AccountServiceImpl getAccountService() {
		return SpringContextHolder.applicationContext.getBean("accountServiceImpl", AccountServiceImpl.class);
	}


	@Override
	public boolean sendCommentMsg(String message) {
//		mqMessageSender.sendMessage(message);
		
		String exchange = ConfigUtil.getMqExchange();
		String routingKey = ConfigUtil.getMqQueueUserCenter();
		
		mqMessageSender.sendMessage(exchange , routingKey, message);		
		return true;
	}

	@Override
	@Async
	public void doHandle(JSONObject data) {
		
		if (data == null) {
			LOG.error("GateWay MQ Message is null");
			return;
		}
		
		int cbType = data.getIntValue("cbType");
		String cbMsg = data.getString("cbMsg");
		switch (cbType) {
		case Callback.CB_CARD_BINDING:			
			RespVerifyBindBankCard respBindBankCard = JSON.parseObject(cbMsg, RespVerifyBindBankCard.class);
			if (respBindBankCard != null) {
				getAccountService().handBindQuickPayCardResult(respBindBankCard);
			}
			break;
		case Callback.CB_DISTRIBUTE:
			
			RespDistribute respDistribute = JSON.parseObject(cbMsg, RespDistribute.class);			
			payCoreService.handDistributeResult(respDistribute, true);			
			break;	
		
		case Callback.CB_REFUND:
			
			RespRefund respRefund = JSON.parseObject(cbMsg, RespRefund.class);
			payCoreService.handRefundResult(respRefund, true);
			break;
			
		case Callback.CB_TRADE:			
			
			RespTrade respTrade = JSON.parseObject(cbMsg, RespTrade.class);
			payCoreService.handPayResult(respTrade, true);
			break;	
			
		case Callback.CB_TRANSFER:			
			
			RespTransfer respTransfer = JSON.parseObject(cbMsg, RespTransfer.class);
			payCoreService.handTransferResult(respTransfer, true);
			break;
			
		case Callback.CB_WITHDRAW:
			
			RespWithdraw respWithdraw = JSON.parseObject(cbMsg, RespWithdraw.class);

			break;
//		case Callback.CB_TRANSFER_BATCH:
//			RespBatchTransfer respBatchTransfer = JSON.parseObject(cbMsg, RespBatchTransfer.class);
//			
//			payCoreService.handBatchTransferResult(respBatchTransfer, true);
//			
//			break;
	
		}	
	
	}
	
	@Override
	@Async
	public void sendTradeFlowMsg(String jsonMessage) {
		String exchange = ConfigUtil.getMqExchange();
		String routingKey = ConfigUtil.getMqQueueTradeCenter();
		mqMessageSender.sendMessage(exchange , routingKey, jsonMessage);
	}

	@Override
	@Async
	public void sendBindCardMsg(BindCardMqMsg bindCardMqMsg,String routingKey) {
		String msg = JSONObject.toJSONString(bindCardMqMsg);
		amqpTemplate.convertAndSend(routingKey,msg);
	}

}
